/**
 * 
 */
package cn.com.xf.service.impl;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.apache.commons.lang.time.DateFormatUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import cn.com.xf.common.Constants;
import cn.com.xf.dao.TblOaFaqDAO;
import cn.com.xf.dao.TblOaNoticeDAO;
import cn.com.xf.dao.TblOaNoticeReadDAO;
import cn.com.xf.dao.TblOaSoftDAO;
import cn.com.xf.dao.TblSiteCompanyDAO;
import cn.com.xf.dao.TblSiteFeedbackDAO;
import cn.com.xf.dao.TblSiteGrayDAO;
import cn.com.xf.dao.TblSiteJobDAO;
import cn.com.xf.dao.TblSiteNewsDAO;
import cn.com.xf.dao.TblSiteProductDAO;
import cn.com.xf.dao.TblSiteResumeDAO;
import cn.com.xf.dao.TblSiteSeoDAO;
import cn.com.xf.dao.TblSiteVedioDAO;
import cn.com.xf.dao.TblbulletinDAO;
import cn.com.xf.dao.TblfaqDAO;
import cn.com.xf.dao.TblfileDAO;
import cn.com.xf.dao.TblnewsDAO;
import cn.com.xf.dao.TblreadDAO;
import cn.com.xf.dao.TblyyrbDAO;
import cn.com.xf.domain.TblOaFaq;
import cn.com.xf.domain.TblOaNotice;
import cn.com.xf.domain.TblOaNoticeRead;
import cn.com.xf.domain.TblOaSoft;
import cn.com.xf.domain.TblSiteCompany;
import cn.com.xf.domain.TblSiteFeedback;
import cn.com.xf.domain.TblSiteGray;
import cn.com.xf.domain.TblSiteJob;
import cn.com.xf.domain.TblSiteNews;
import cn.com.xf.domain.TblSiteProduct;
import cn.com.xf.domain.TblSiteResume;
import cn.com.xf.domain.TblSiteSeo;
import cn.com.xf.domain.TblSiteVedio;
import cn.com.xf.domain.Tblbulletin;
import cn.com.xf.domain.Tblfaq;
import cn.com.xf.domain.Tblfile;
import cn.com.xf.domain.Tblnews;
import cn.com.xf.domain.Tblread;
import cn.com.xf.domain.Tblyyrb;
import cn.com.xf.service.SiteService;
import cn.com.xf.web.form.SiteForm;
import cn.com.xf.web.security.UserInfo;

/**
 * @author Administrator
 * 
 */
@Service
public class SiteServiceImpl implements SiteService {
	@Autowired
	private TblSiteProductDAO tblSiteProductDAO;
	@Autowired
	private TblSiteCompanyDAO tblSiteCompanyDAO;
	@Autowired
	private TblSiteNewsDAO tblSiteNewsDAO;
	@Autowired
	private TblSiteVedioDAO tblSiteVedioDAO;
	@Autowired
	private TblSiteJobDAO tblSiteJobDAO;
	@Autowired
	private TblSiteFeedbackDAO tblSiteFeedbackDAO;
	@Autowired
	private TblSiteSeoDAO tblSiteSeoDAO;
	@Autowired
	private TblnewsDAO tblnewsDAO;
	@Autowired
	private TblfileDAO tblfileDAO;
	@Autowired
	private TblOaSoftDAO tblOaSoftDAO;
	@Autowired
	private TblbulletinDAO tblbulletinDAO;
	@Autowired
	private TblyyrbDAO tblyyrbDAO;
	@Autowired
	private TblOaNoticeDAO tblOaNoticeDAO;
	@Autowired
	private TblfaqDAO tblfaqDAO;
	@Autowired
	private TblOaFaqDAO tblOaFaqDAO;
	@Autowired
	private TblSiteGrayDAO tblSiteGrayDAO;
	@Autowired
	private TblSiteResumeDAO tblSiteResumeDAO;
	@Autowired
	private TblreadDAO tblreadDAO;
	@Autowired
	private TblOaNoticeReadDAO tblOaNoticeReadDAO;

	@Override
	public TblSiteProduct selectProductByType(String productType) {
		return tblSiteProductDAO.selectByType(productType);
	}

	@Override
	public TblSiteCompany selectCompanyByType(String type) {
		return tblSiteCompanyDAO.selectByType(type);
	}

	@Override
	public void updateProductByType(String productType, String content, String contentEn, String contentBig5) {
		TblSiteProduct oldProd = tblSiteProductDAO.selectByType(productType);
		oldProd.setDescribe(content);
		oldProd.setDescribeEn(contentEn);
		oldProd.setDescribeBig5(contentBig5);
		oldProd.setUpdatetime(new Date());
		UserInfo user = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		oldProd.setUpdateuser(user.getName());
		tblSiteProductDAO.updateByPrimaryKeyWithBLOBs(oldProd);
	}

	@Override
	public void updateCompanyByType(String companyType, String content, String contentEn, String contentBig5) {
		TblSiteCompany company = tblSiteCompanyDAO.selectByType(companyType);
		company.setContent(content);
		company.setContentEn(contentEn);
		company.setContentBig5(contentBig5);
		company.setUpdatetime(new Date());
		UserInfo user = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		company.setUpdateuser(user.getName());
		tblSiteCompanyDAO.updateByPrimaryKeyWithBLOBs(company);
	}

	@Override
	public int insertNews(TblSiteNews news) {
		String name = SecurityContextHolder.getContext().getAuthentication().getName();
		Date addDate = new Date();
		news.setAddtime(addDate);
		news.setAdduser(name);
		tblSiteNewsDAO.insert(news);
		return 0;
	}

	@Override
	public List<TblSiteNews> selectNews(int begin, int pageSize, String searchField, String searchValue,
			String orderByField, String orderByMode, String isimportant, String istop, String type, String isShow) {
		List<TblSiteNews> list = tblSiteNewsDAO.selectAndOrder(begin, pageSize, searchField, searchValue, orderByField,
				orderByMode, isimportant, istop, type, isShow);
		return list;
	}

	@Override
	public int selectNewsCount(String field, String value, String isimportant, String istop, String type, String isShow) {
		return tblSiteNewsDAO.selectCount(field, value, isimportant, istop, type, isShow);
	}

	@Override
	public List<TblSiteNews> selectAll() {
		return tblSiteNewsDAO.selectAll();
	}

	@Override
	public TblSiteNews selectNewsById(Integer id) {
		TblSiteNews news = tblSiteNewsDAO.selectByPrimaryKey(new BigDecimal(Integer.parseInt(id.toString())));
		return news;

	}

	@Override
	public int updateNews(TblSiteNews news) {
		return tblSiteNewsDAO.updateByPrimaryKey(news);

	}

	@Override
	public int deleteNews(BigDecimal id) {

		return tblSiteNewsDAO.deleteByPrimaryKey(id);
	}

	@Override
	public int insertVideo(TblSiteVedio video) {
		UserInfo user = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		video.setAddtime(new Date());
		video.setAdduser(user.getName());
		tblSiteVedioDAO.insert(video);
		return 0;
	}

	@Override
	public List<TblSiteVedio> selectVideos() {

		return tblSiteVedioDAO.selectVedioList();
	}

	@Override
	public int selectVideoCount() {
		return tblSiteVedioDAO.selectCount();
	}

	@Override
	public int deleteVideo(Integer id) {
		int rows = tblSiteVedioDAO.deleteByPrimaryKey(new BigDecimal(id));
		return rows;
	}

	@Override
	public int updateVideo(TblSiteVedio video) {
		int rows = tblSiteVedioDAO.updateByPrimaryKey(video);
		return rows;
	}

	@Override
	public TblSiteVedio selectVideoById(Integer id) {
		return tblSiteVedioDAO.selectByPrimaryKey(new BigDecimal(id));
	}

	@Override
	public List<TblSiteVedio> selectVideos(int begin, int pageSize, String orderByField, String orderByMode) {
		List<TblSiteVedio> list = tblSiteVedioDAO.selectAndOrder(begin, pageSize, orderByField, orderByMode);
		return list;
	}

	@Override
	public int insertJob(TblSiteJob job) {
		UserInfo user = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		job.setAddtime(new Date());
		job.setAdduser(user.getName());
		tblSiteJobDAO.insert(job);
		return 0;
	}

	@Override
	public int selectJobCount() {
		return tblSiteJobDAO.selectCount();
	}

	@Override
	public List<TblSiteJob> selectJobs() {
		return tblSiteJobDAO.selectAllJobs();
	}

	@Override
	public TblSiteJob selectJobById(Integer id) {
		return tblSiteJobDAO.selectByPrimaryKey(new BigDecimal(id));
	}

	@Override
	public int deleteJob(Integer id) {
		int rows = tblSiteJobDAO.deleteByPrimaryKey(new BigDecimal(id));
		return rows;
	}

	@Override
	public int updateJob(TblSiteJob job) {
		int rows = tblSiteJobDAO.updateByPrimaryKey(job);
		return rows;
	}

	@Override
	public List<TblSiteJob> selectJob(int begin, int pageSize) {
		return tblSiteJobDAO.selectAndOrder(begin, pageSize);
	}

	@Override
	public int selectFeedbackCount(String name, String orderId, String content) {
		return tblSiteFeedbackDAO.selectCount(name, orderId, content);
	}

	@Override
	public List<TblSiteFeedback> selectFeedBack(int begin, int pageSize, String orderByField, String orderByMode,
			String name, String orderId, String content) {
		return tblSiteFeedbackDAO.selectAndOrder(begin, pageSize, orderByField, orderByMode, name, orderId, content);
	}

	@Override
	public TblSiteFeedback selectFeedbackById(Integer id) {
		return tblSiteFeedbackDAO.selectByPrimaryKey(new BigDecimal(id));
	}

	@Override
	public List<TblSiteSeo> selectSiteSeoList() {
		return tblSiteSeoDAO.selectList();
	}

	@Override
	public int updateSiteSeo(String id, SiteForm form) {
		TblSiteSeo old = tblSiteSeoDAO.selectByPrimaryKey(new BigDecimal(id));
		int result = -1;
		if (null != old) {
			old.setTitle(form.getPageTitle().trim());
			old.setKeywords(form.getPageKeywords().trim());
			old.setDescription(form.getPageDescription().trim());
			result = tblSiteSeoDAO.updateByPrimaryKey(old);
		}
		return result;
	}

	@Override
	public TblSiteSeo selectSiteSeoById(String id) {
		return tblSiteSeoDAO.selectByPrimaryKey(new BigDecimal(id));
	}

	@Override
	public int updateForCopyAllOldData(String contextPath) {
		System.out.println("开始复制老数据......");
		UserInfo user = (UserInfo) SecurityContextHolder.getContext().getAuthentication().getPrincipal();
		// 一.新闻复制
		System.out.println("开始复制老数据:新闻......");
		List<Tblnews> oldNewList = tblnewsDAO.selectAll();
		if (!CollectionUtils.isEmpty(oldNewList)) {
			for (Tblnews old : oldNewList) {
				TblSiteNews news = new TblSiteNews();
				String contains = old.getFldcontains();
				if (StringUtils.isNotBlank(contains)) {
					contains = contains.replaceAll("http://www.xf-express.com.cn/DownFiles/", contextPath
							+ "/imagextra/");
					contains = contains.replaceAll("http://www.xf-express.com.cn/Downfiles/", contextPath
							+ "/imagextra/");
					contains = contains.replaceAll("/DownFiles/", contextPath + "/imagextra/");
					contains = contains.replaceAll("/Downfiles/", contextPath + "/imagextra/");
				}
				news.setContent(contains);
				news.setIsImportant("0");
				news.setIsTop("0");
				news.setIsShow(old.getFldonlineb() + "");
				news.setNewsAbstract("");
				news.setNewsSource("");
				news.setPicture("");
				news.setPublicMan(old.getFldpublishers());
				news.setPublicTime(old.getFldpubdatet());
				news.setPublicTimeStr(DateFormatUtils.format(old.getFldpubdatet(), "yyyy-MM-dd HH:mm:ss"));
				news.setTitle(old.getFldtitles());
				String type = "0";
				if ("行业新闻".equalsIgnoreCase(old.getFldclasss())) {
					type = "1";
				}
				news.setType(type);
				news.setAddtime(new Date());
				news.setAdduser(user.getName());
				tblSiteNewsDAO.insertSelective(news);
			}
		}
		// 二.软件复制
		System.out.println("开始复制老数据:软件......");
		List<Tblfile> oldFileList = tblfileDAO.selectAll();
		if (!CollectionUtils.isEmpty(oldFileList)) {
			for (Tblfile old : oldFileList) {
				TblOaSoft soft = new TblOaSoft();
				soft.setFileName(old.getFldnames());

				String desc = old.getFlddescs();
				if (StringUtils.isNotBlank(desc)) {
					desc = desc.replaceAll("http://www.xf-express.com.cn/DownFiles/", contextPath + "/imagextra/");
					desc = desc.replaceAll("http://www.xf-express.com.cn/Downfiles/", contextPath + "/imagextra/");
					desc = desc.replaceAll("/DownFiles/", contextPath + "/imagextra/");
					desc = desc.replaceAll("/Downfiles/", contextPath + "/imagextra/");
				}
				soft.setFileDesc(desc);
				soft.setPublicDate(old.getFldpubdatet());
				soft.setDateStr(DateFormatUtils.format(old.getFldpubdatet(), "yyyy-MM-dd HH:mm:ss"));
				soft.setPublicMan(old.getFldpublishers());
				soft.setDownfileName(old.getFlddownfilenames());
				soft.setFileSize(old.getFldsized());
				soft.setTypeId(new BigDecimal(old.getFldtypeidd() + ""));
				String fileType = Constants.SOFT_TYPE_MAP.get(old.getFldtypeidd() + "");
				soft.setFileType(fileType);

				soft.setAddTime(new Date());
				soft.setAddUser(user.getName());
				tblOaSoftDAO.insertSelective(soft);
			}
		}
		// 三.内部公告营运日报
		System.out.println("开始复制老数据:内部公告......");
		List<Tblbulletin> oldBulletinList = tblbulletinDAO.selectAll();
		List<Tblyyrb> oldYyrbList = tblyyrbDAO.selectAll();
		if (!CollectionUtils.isEmpty(oldBulletinList)) {
			for (int i = 0; i < oldBulletinList.size(); i++) {
				// for (int i = 0; i < 2; i++) {
				Tblbulletin old = oldBulletinList.get(i);
				TblOaNotice notice = new TblOaNotice();
				notice.setTitle(old.getFldggtitles());
				String desc = old.getFldggcontents();
				if (StringUtils.isNotBlank(desc)) {
					desc = desc.replaceAll("http://www.xf-express.com.cn/DownFiles/", contextPath + "/filextra/");
					desc = desc.replaceAll("http://www.xf-express.com.cn/Downfiles/", contextPath + "/filextra/");
					desc = desc.replaceAll("/DownFiles/", contextPath + "/filextra/");
					desc = desc.replaceAll("/Downfiles/", contextPath + "/filextra/");

					Document doc = Jsoup.parse(desc);
					Elements imgEles = doc.select("img");
					for (Element e : imgEles) {
						if (e.hasAttr("src")) {
							String src = e.attr("src");
							if (src.endsWith(".jpg") || src.endsWith(".png") || src.endsWith(".gif")) {
								src = src.replace("/filextra/", "/imagextra/");
								e.attr("src", src);
							}
						}
					}
					desc = doc.body().html();
				}
				notice.setContent(desc);
				notice.setPublicMan(old.getFldfromusers());
				notice.setCenterSite("769000");
				notice.setIsimportant("0");
				notice.setIstop("0");
				notice.setNoticeGroup("0");

				notice.setSendto(old.getFldtos());
				notice.setCopyto(old.getFldccs());
				notice.setPublicDepart(old.getFldfroms());
				notice.setDocNumber(old.getFldnos());
				notice.setPublicTime(old.getFldpubdatet());
				notice.setPublicTimeStr(DateFormatUtils.format(old.getFldpubdatet(), "yyyy-MM-dd HH:mm:ss"));

				notice.setAddUser(user.getName());
				notice.setAddTime(new Date());
				int noticeId = Integer.parseInt(tblOaNoticeDAO.insertSelective(notice).toString());
				// 导入阅读记录
				List<Tblread> readList = tblreadDAO.selectById(old.getFldidd() + "");
				if (!CollectionUtils.isEmpty(readList)) {
					List<String> ucReadList = new ArrayList<String>();
					for (Tblread r : readList) {
						if (StringUtils.isNotBlank(r.getEmpId())) {
							if (!ucReadList.contains(r.getEmpId())) {
								ucReadList.add(r.getEmpId());
								TblOaNoticeRead read = new TblOaNoticeRead();
								read.setNoticeid(Integer.valueOf(noticeId));
								read.setEmpid(r.getEmpId());
								tblOaNoticeReadDAO.insert(read);
							}
						}
					}
				}
				System.out.println("完成第" + (i + 1) + "条公告导入，共" + oldBulletinList.size() + "条公告");
			}
		}
		System.out.println("开始复制老数据:营运日报......");
		if (!CollectionUtils.isEmpty(oldYyrbList)) {
			for (int i = 0; i < oldYyrbList.size(); i++) {
				// for (int i = 0; i < 1; i++) {
				Tblyyrb yyrb = oldYyrbList.get(i);
				TblOaNotice notice = new TblOaNotice();
				notice.setTitle(yyrb.getFldggtitles());

				String desc = yyrb.getFldggcontents();
				if (StringUtils.isNotBlank(desc)) {
					desc = desc.replaceAll("http://www.xf-express.com.cn/DownFiles/", contextPath + "/filextra/");
					desc = desc.replaceAll("http://www.xf-express.com.cn/Downfiles/", contextPath + "/filextra/");
					desc = desc.replaceAll("/DownFiles/", contextPath + "/filextra/");
					desc = desc.replaceAll("/Downfiles/", contextPath + "/filextra/");

				}
				notice.setContent(desc);
				notice.setPublicMan(yyrb.getFldfromusers());
				notice.setCenterSite("769000");
				notice.setIsimportant("0");
				notice.setIstop("0");
				notice.setNoticeGroup("1");
				notice.setPublicTime(yyrb.getFldpubdatet());
				notice.setPublicTimeStr(DateFormatUtils.format(yyrb.getFldpubdatet(), "yyyy-MM-dd HH:mm:ss"));
				notice.setAddUser(user.getName());
				notice.setAddTime(new Date());
				tblOaNoticeDAO.insertSelective(notice);
			}
		}
		// 四.FAQ
		System.out.println("开始复制老数据:Faq......");
		List<Tblfaq> faqList = tblfaqDAO.selectAll();
		if (!CollectionUtils.isEmpty(faqList)) {
			for (Tblfaq old : faqList) {
				TblOaFaq faq = new TblOaFaq();
				faq.setAddTime(old.getFldaddtimet());
				faq.setAddUser(old.getFldpublishers());
				faq.setFaqAnswer(old.getFldanswers());
				faq.setFaqClass(old.getFldclasss());
				faq.setFaqOnlineb(Integer.valueOf(old.getFldonlineb() + ""));
				faq.setFaqQuestion(old.getFldquerys());
				faq.setPublicMan(old.getFldpublishers());
				faq.setPublicTime(old.getFldaddtimet());
				tblOaFaqDAO.insertSelective(faq);
			}
		}
		System.out.println("开始复制老数据:全部复制完成");

		return 0;
	}

	@Override
	public int updateSiteGray(TblSiteGray siteGray) {
		tblSiteGrayDAO.updateByPrimaryKey(siteGray);
		return 0;
	}

	@Override
	public TblSiteGray selectSiteGray() {
		return tblSiteGrayDAO.selectByPrimaryKey("1");
	}

	@Override
	public int selectResumeCount(String jobName, String createDateBegin, String createDateEnd) {
		return tblSiteResumeDAO.selectCount(jobName, createDateBegin, createDateEnd);
	}

	@Override
	public TblSiteResume selectResumeById(String id) {
		return tblSiteResumeDAO.selectByPrimaryKey(new BigDecimal(id));
	}

	@Override
	public List<TblSiteResume> selectResume(int begin, int pageSize, String orderByField, String orderByMode,
			String jobName, String createDateBegin, String createDateEnd) {
		return tblSiteResumeDAO.selectAndOrder(begin, pageSize, orderByField, orderByMode, jobName, createDateBegin,
				createDateEnd);
	}

	@Override
	public int deleteResumeById(String id) {
		return tblSiteResumeDAO.deleteByPrimaryKey(new BigDecimal(id));
	}

}
